//
//  OMPersonalCenterCategoryTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/20.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMPersonalCenterCategoryTableCell.h"
#import "OMPersonalCenterCategoryCollectCell.h"
#import "Navigator.h"
@interface OMPersonalCenterCategoryTableCell () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectView;
@property (nonatomic, strong) NSArray *items;
@end

@implementation OMPersonalCenterCategoryTableCell

- (void)configure
{
    [super configure];
    [self.contentView addSubview:self.collectView];
    [self.collectView registerClass:[OMPersonalCenterCategoryCollectCell class]
         forCellWithReuseIdentifier:[OMPersonalCenterCategoryCollectCell cellIdentifier]];
    [self setupConstraints];

    self.items = @[
                   @{@"name":@"我的地址",@"image":@"wodedizhi_25x25_",@"controller":@"OMDeliveryAddressViewController",@"needLogin":@(YES)},
                   @{@"name":@"意见反馈",@"image":@"yijianfankui_25x25_",@"controller":@"OMAdviseSubmitViewController",@"needLogin":@(NO)},
                   @{@"name":@"粉丝专区",@"image":@"dianfenzhuanqu_25x25_",@"controller":@"BaseTableViewController",@"needLogin":@(NO)},
                   @{@"name":@"商家合作",@"image":@"shangjiajiameng_25x25_",@"controller":@"BaseTableViewController",@"needLogin":@(NO)},
                   @{@"name":@"欢迎评分",@"image":@"guiqiuhaoping_25x25_",@"controller":@"BaseTableViewController",@"needLogin":@(NO)},
                   @{@"name":@"疑问帮助",@"image":@"yiwenbangzhu_25x25_",@"controller":@"BaseTableViewController",@"needLogin":@(NO)},
                   @{@"name":@"关于我们",@"image":@"guanyuwomen_25x25_",@"controller":@"OMAboutViewController",@"needLogin":@(NO)},
                   @{@"name":@"设置",@"image":@"setting_25x25_",@"controller":@"OMConfigureViewController",@"needLogin":@(NO)},
                   ];

}

- (void)setupConstraints
{
    [self.collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(@0);
    }];

    UIView *line = nil;
    for (NSUInteger i = 0; i < 3; i ++) {
        line = [UIView new];
        line.backgroundColor = SM_naviShadowColor;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@.5);
            make.left.equalTo(@(OMPersonalCenterCategoryCollectCell_size*(i+1)));
            make.top.bottom.equalTo(@0);
        }];
    }

    for (NSUInteger i = 0; i < 1; i ++) {
        line = [UIView new];
        line.backgroundColor = SM_naviShadowColor;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@.5);
            make.top.equalTo(@(OMPersonalCenterCategoryCollectCell_size*(i+1)));
            make.left.right.equalTo(@0);
        }];
    }
}



#pragma mark - Getters

- (UICollectionView *)collectView
{
    if (!_collectView) {
        _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero
                                         collectionViewLayout:[self layout]];
        _collectView.delegate = self;
        _collectView.dataSource = self;
        _collectView.backgroundColor = SM_whiteColor;
        _collectView.scrollEnabled = NO;

    }
    return _collectView;
}

- (UICollectionViewLayout*)layout
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = .5;
    flowLayout.minimumLineSpacing = .5;
    float itemSize = OMPersonalCenterCategoryCollectCell_size;
    flowLayout.itemSize = CGSizeMake(itemSize, itemSize);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    return flowLayout;
}


#pragma mark - collect view delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMPersonalCenterCategoryCollectCell *cell = [OMPersonalCenterCategoryCollectCell collectCellWithCollectionView:collectionView atIndexPath:indexPath];
    NSString *imageStr = self.items[indexPath.item][@"image"];
    cell.imageView.image = imageStr.length?[UIImage imageNamed:imageStr]:[UIImage imageWithColor:SM_whiteColor];
    cell.label.text = self.items[indexPath.item][@"name"];

    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *itemDic = self.items[indexPath.item];
    BOOL needLogin = [itemDic[@"needLogin"] boolValue];
    if ( !needLogin || (needLogin&&[Navigator showLoginViewIfUnLogin])) {
        if ([itemDic[@"name"] isEqualToString:@"欢迎评分"]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/us/app/id1213719192"]];
        }
        else if ([itemDic[@"controller"] length]) {
            NaviPush(itemDic[@"controller"], @{@"title":itemDic[@"name"]});
        }
    }
}

+ (CGFloat)cellHeight
{
    return OMPersonalCenterCategoryCollectCell_size*2+0.5*(2-1);
}
@end
